//
//  StatusCell.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/12.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    
    //声明底部－顶部的约束属性对象
    var bottomToTopConstraint: Constraint?
    
    //模型数据
    var status: Status? {
        didSet {
            //绑定模型数据
            oringnalView.status = status
            
            //卸载约束对象
            bottomToTopConstraint?.uninstall()
            
//判断是不是转发微博 给转发微博赋值     复杂的算法而不是用三目运算符 是为了更好说明这个地方有复用的问题
            if let weibo = status?.retweeted_status {
                
                retweetedView.retweetedStatus = weibo
                
                //修改约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomToTopConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
                //显示转发微博
                retweetedView.hidden = false
                
            }
            else {
                retweetedView.retweetedStatus = nil
                
                //相对于原创微博更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomToTopConstraint = make.top.equalTo(oringnalView.snp_bottom).constraint
                })
                
                //隐藏转发微博
                retweetedView.hidden = true
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置选中状态 
        selectionStyle = .None
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI
    private func setupUI() {
        //添加子视图 

        contentView.addSubview(oringnalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        //设置约束
        oringnalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //测试高度
//            make.height.equalTo(100)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(oringnalView.snp_bottom)
            make.left.right.equalTo(oringnalView)
        }
      
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            self.bottomToTopConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(36)
        }
        //开启自动布局后 所有的frame布局都会自动失效
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    //原创微博视图
    private lazy var oringnalView: StatusOringnalView = StatusOringnalView()
    //微博底部视图
    private lazy var bottomView: StatusBottomView = StatusBottomView()
    //转发微博
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()
 
}


/*

//从xib  / sb 中加载的cell 才会执行这个方法
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
*/






















